package cn.training.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Slf4j
@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {
	@Autowired
	HelloWorldService helloWorldService;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
        return "denglu";
    }
@RequestMapping(value = "/init", method = RequestMethod.POST)
public String initLogin(UserBean UserBean,Model model) {
    	UserBean result=helloWorldService.searchUser(UserBean);
    	model.addAttribute("UserBean",result);
        return "login";
    }
@RequestMapping(value = "/name", method = RequestMethod.POST)
public String nameLogin(UserBean UserBean,Model model) {
    	UserBean result=helloWorldService.searchname(UserBean);
    	model.addAttribute("UserBean",result);
        return "login";
    }
//注册页面
@RequestMapping(value = "/aa", method = RequestMethod.POST,params="zhuce")
public String aaLogin(HttpServletRequest req,UserBean UserBean,Model model)  {
    String password = req.getParameter("password");
    String repassword = req.getParameter("repassword");
    if(password.equals(repassword)){
       if(helloWorldService.insertUser(UserBean)!=0){
    	model.addAttribute("xinxi", "已注册成功！");
    	return "zhuce" ;
    }
       else
    	return "zhuce";	
    	}
    else{
    	model.addAttribute("xinxi", "两次输入的密码不一样！");
    	return "zhuce" ;}
    }
//更新用户信息
@RequestMapping(value = "/update", method = RequestMethod.GET)
public String update(UserBean UserBean,Model model) {
    	UserBean result=helloWorldService.sUser(UserBean);
    	model.addAttribute("UserBean",result);
        return "update";
    }
@RequestMapping(value = "/tupdate", method = RequestMethod.POST)
public String Login(HttpServletRequest req,Model model,@Valid @ModelAttribute("UserBean") UserBean UserBean,BindingResult results) throws SQLException {
	if (results.hasErrors()) {
		log.info("内容验证出错");
		model.addAttribute("xinxi", "用户名或密码不能为空！");
		UserBean result=helloWorldService.sUser(UserBean);
    	model.addAttribute("UserBean",result);
        return "update";
	}
	if (!UserBean.getPassword().equals(UserBean.getRepassword())) {
		log.info("密码验证出错");
		model.addAttribute("xinxi", "两次输入的密码不一样！");
		UserBean result=helloWorldService.sUser(UserBean);
    	model.addAttribute("UserBean",result);
        return "update";
		}
	 if(helloWorldService.updateUser(UserBean)!=0){
    		List<UserBean> result=helloWorldService.listUser(UserBean);
        	model.addAttribute("list",result);
            return "list";
            }
	 else
		 return "update";
  }
//@RequestMapping(value = "/tupdate", method = RequestMethod.POST)
//public String Login(HttpServletRequest req,Model model, UserBean UserBean) {
//	String password = req.getParameter("password");
//	String repassword = req.getParameter("repassword");
//	String username = req.getParameter("username");
//	if(username.isEmpty()||password.isEmpty()){
//		UserBean result=helloWorldService.sUser(UserBean);
//    	model.addAttribute("UserBean",result);
//    	model.addAttribute("xinxi", "用户名或密码不能为空！");
//		log.info("内容验证出错");
//		return "update";
//	}
//	
//	if (!UserBean.getPassword().equals(repassword)) {
//		UserBean result=helloWorldService.sUser(UserBean);
//    	model.addAttribute("UserBean",result);
//		log.info("密码验证出错");
//		model.addAttribute("xinxi", "两次输入的密码不一样！");
//		return "update";
//	}
//	if(password.equals(repassword)){
//    	  if(helloWorldService.updateUser(UserBean)!=0){
//    		List<UserBean> result=helloWorldService.listUser(UserBean);
//        	model.addAttribute("list",result);
//            return "list";
//    	}
//    	  else
//    		return"update";
//	    }
//	    else
//	    {
//	    
//        return "update";}
//}

//@RequestMapping(value = "/tupdate", method = RequestMethod.POST)
//public String Login(HttpServletRequest req,UserBean UserBean,Model model) {
//	 String password = req.getParameter("password");
//	    String repassword = req.getParameter("repassword");
//	    if(password.equals(repassword)){
//    	  if(helloWorldService.updateUser(UserBean)!=0){
//    		List<UserBean> result=helloWorldService.listUser(UserBean);
//        	model.addAttribute("list",result);
//            return "list";
//    	}
//    	  else
//    		return"update";
//	    }
//	    else
//	    {
//	    UserBean result=helloWorldService.sUser(UserBean);
//    	model.addAttribute("UserBean",result);
//    	model.addAttribute("xinxi", "两次输入的密码不一样！");
//        return "update";}
//}
    //登录页面
    @RequestMapping(value = "/list", method = RequestMethod.POST,params="login")
    public String listLogin(UserBean UserBean,Model model) {
    	UserBean re=helloWorldService.rlistUser(UserBean);
    	if(re!=null){
    	List<UserBean> result=helloWorldService.listUser(UserBean);
    	model.addAttribute("list",result);
        return "list";}
        else {
        	model.addAttribute("message", "用户ID或密码错误！");
        	return "denglu";}
        } 
    @RequestMapping(value = "/list", method = RequestMethod.POST,params="zhuce")
    public String back(Model model) {
        return "zhuce";
    }
    @RequestMapping(value = "/aa", method = RequestMethod.POST,params="dc")
    public String zhuce(Model model) {
        return "denglu";
    }
   
    @RequestMapping(value = "/queren", method = RequestMethod.GET)
    public String queren(UserBean UserBean,Model model) {
    	UserBean result=helloWorldService.searchdUser(UserBean);
    	model.addAttribute("UserBean",result);
        return "queren";
    }
    //确认页面
    @RequestMapping(value = "/delete", method = RequestMethod.POST,params="YES")
    public String delete(UserBean UserBean,Model model) {
    	int result1=helloWorldService.deleteUser(UserBean);//差删除的判断
    	model.addAttribute("list",result1);
    	List<UserBean> result=helloWorldService.listUser(UserBean);
    	model.addAttribute("list",result);
        return "list";
    }
    @RequestMapping(value = "/delete", method = RequestMethod.POST,params="NO")
    public String fanhui(UserBean UserBean,Model model) {
    	
    	List<UserBean> result=helloWorldService.listUser(UserBean);
    	model.addAttribute("list",result);
        return "list";
    }  
}
